package com.sljy.hzk.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sljy.hzk.R;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.ExamHistotyEntity;
import com.sljy.hzk.entity.ExamInfosEntity;
import com.sljy.hzk.entity.ExamMainEntity;
import com.sljy.hzk.entity.UserEntity;
import com.sljy.hzk.net.BaseCallBack;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.ExamService;
import com.sljy.hzk.ui.exam.ExamDoActivity;
import com.sljy.hzk.ui.exam.ExamErrorRecordActivity__JumpCenter;
import com.sljy.hzk.ui.exam.ExamLoadingActivity;
import com.sljy.hzk.ui.exam.ExamMainActivity;
import com.sljy.hzk.util.TimeUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created  on 2018/7/4 0004
 *
 * @author 唐开阔
 * @describe 考试安排 adapter
 * 某一天没有数据先空着。
 */
public class ExamHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<ExamHistotyEntity.ExamInfo> examListBeans;
    private Context context;
    private final static int TYPE_ITEM = 1;//正常项
    private final static int TYPE_HEAD = 2;//日期头部

    public ExamHistoryAdapter(List<ExamHistotyEntity.ExamInfo> examListBeans, Context context) {
        this.examListBeans = examListBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exam_history, parent, false);
                return new ItemViewHolder(view);
            case TYPE_HEAD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exam_history_head, parent, false);
                return new HeadViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_ITEM:
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                itemViewHolder.bindData(examListBeans.get(position));
                break;
            case TYPE_HEAD:
                HeadViewHolder headViewHolder = (HeadViewHolder) holder;
                headViewHolder.bindData(TimeUtils.getMonth(examListBeans.get(position + 1).getExamDate()));
                break;
        }
    }

    /**
     * 判断头部的位置：前提-数据已经按照时间排序
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (examListBeans.get(position).getExamDate().equals("-1")) {
            return TYPE_HEAD;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return examListBeans.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.summitTime)
        TextView summitTime;
        @BindView(R.id.stateBg)
        ImageView stateBg;
        @BindView(R.id.tv_pos)
        TextView tvPos;
        @BindView(R.id.tv_score)
        TextView tvFenshu;
        @BindView(R.id.bodyState)
        ImageView bodyState;
        @BindView(R.id.layout_root)
        RelativeLayout rootLayout;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

        @SuppressLint("SetTextI18n")
        public void bindData(ExamHistotyEntity.ExamInfo data) {
            tvName.setText(data.getExamName());
            tvPos.setText(data.getPartitionName());
            tvDate.setText(data.getExamDate() + "  ");
            tvFenshu.setText(String.valueOf(data.getScore()+"分"));
            summitTime.setText(data.getSubmitDate());
            if(data.getSignInStatus().equals("未签到")){
                stateBg.setBackgroundResource(R.drawable.none);
                bodyState.setBackgroundResource(R.drawable.absencefromduty);
                tvFenshu.setVisibility(View.GONE);
            }else if(data.getSignInStatus().equals("已签到")){
                stateBg.setBackgroundResource(R.drawable.signin);
                if(!data.getTestStatus().equals("已考试")){
                    bodyState.setBackgroundResource(R.drawable.missanexam);
                    tvFenshu.setVisibility(View.GONE);
                }else{
                    bodyState.setVisibility(View.GONE);
                }
            }
            if(Integer.parseInt( data.getScore()) >= data.getQuaStandardRate()){
                tvFenshu.setBackgroundResource(R.drawable.qualified);
            }else{
                tvFenshu.setBackgroundResource(R.drawable.unqualified);
            }
            rootLayout.setOnClickListener((v -> {
                ExamErrorRecordActivity__JumpCenter.builder(context)
                        .setExamId(Integer.parseInt(data.getExamId()))
                        .setScore(Integer.parseInt(data.getScore()))
                        .create().go();

            }));

        }
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(String time) {
            tvName.setText(time);

        }
    }
}
